// 成就系统 - 管理游戏成就
class AchievementSystem {
    constructor(scene) {
        this.scene = scene;

        // 定义所有成就
        this.achievements = [
            { id: 'wave5', name: '初级生存者', description: '存活5波', completed: false },
            { id: 'wave10', name: '中级生存者', description: '存活10波', completed: false },
            { id: 'wave20', name: '高级生存者', description: '存活20波', completed: false },
            { id: 'boss1', name: 'Boss猎人', description: '击败第一个Boss', completed: false },
            { id: 'boss3', name: 'Boss终结者', description: '击败3个Boss', completed: false },
            { id: 'allWeapons', name: '武器收藏家', description: '获得所有武器', completed: false },
            { id: 'kills100', name: '百人斩', description: '击杀100个敌人', completed: false },
            { id: 'kills500', name: '屠夫', description: '击杀500个敌人', completed: false },
            { id: 'exp1000', name: '经验丰富', description: '收集1000点经验', completed: false }
        ];

        // 加载已完成的成就
        this.loadAchievements();
    }

    // 加载成就进度
    loadAchievements() {
        const savedData = localStorage.getItem('pixelSurvivorAchievements');
        if (savedData) {
            const completedAchievements = JSON.parse(savedData);
            this.achievements.forEach(achievement => {
                if (completedAchievements[achievement.id]) {
                    achievement.completed = true;
                }
            });
        }
    }

    // 保存成就进度
    saveAchievements() {
        const completedAchievements = {};
        this.achievements.forEach(achievement => {
            if (achievement.completed) {
                completedAchievements[achievement.id] = true;
            }
        });
        localStorage.setItem('pixelSurvivorAchievements', JSON.stringify(completedAchievements));
    }

    // 解锁成就
    unlockAchievement(achievementId, name, description) {
        const achievement = this.achievements.find(a => a.id === achievementId);
        if (achievement && !achievement.completed) {
            achievement.completed = true;
            this.saveAchievements();
            EffectSystem.showAchievementUnlocked(this.scene, name, description);
        }
    }

    checkAchievements() {
        // 检查波次成就
        if (this.scene.wave >= 5) {
            this.unlockAchievement('wave5', '初级生存者', '存活5波');
        }

        if (this.scene.wave >= 10) {
            this.unlockAchievement('wave10', '中级生存者', '存活10波');
        }

        if (this.scene.wave >= 20) {
            this.unlockAchievement('wave20', '高级生存者', '存活20波');
        }

        // 检查Boss成就
        if (this.scene.bossesKilled >= 1) {
            this.unlockAchievement('boss1', 'Boss猎人', '击败第一个Boss');
        }

        if (this.scene.bossesKilled >= 3) {
            this.unlockAchievement('boss3', 'Boss终结者', '击败3个Boss');
        }

        // 检查武器成就
        const allWeapons = ['pistol', 'shotgun', 'rifle', 'machinegun', 'laser', 'rocket', 'electric'];
        const hasAllWeapons = allWeapons.every(weapon => this.scene.weapons.includes(weapon));

        if (hasAllWeapons) {
            this.unlockAchievement('allWeapons', '武器收藏家', '获得所有武器');
        }

        // 检查击杀成就
        if (this.scene.enemiesKilled >= 100) {
            this.unlockAchievement('kills100', '百人斩', '击杀100个敌人');
        }

        if (this.scene.enemiesKilled >= 500) {
            this.unlockAchievement('kills500', '屠夫', '击杀500个敌人');
        }

        // 检查经验成就
        if (this.scene.experiencePoints >= 1000) {
            this.unlockAchievement('exp1000', '经验丰富', '收集1000点经验');
        }
    }
}